package no.uio.pussle.chat.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

/**
 * 
 * @author JanOleOS and KyrreHe
 *
 */
public class main extends Activity {
//test
	/**
	 * The hardcoded IP to a sever running on Smaragd.ifi.uio.no
	 * Will be up for a very long time...
	 * PS: Don't hardcode
	 */
	String ip = "129.240.65.58";
	String nick;

	/** 
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		/**
		 * Creating Button variable to hold our chat button from main.xml
		 * Then we set the OnClickListener which let us manage clicking
		 */
		Button chat = (Button) findViewById(R.id.chat);
		chat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				/**
				 * An intent is used for communication between Activities.
				 */
				final Intent intent = new Intent().setClass(v.getContext(), Chat.class);
				intent.putExtra("IP", ip);
				
				/**
				 * Calls AlertDialog to create a little pop up dialog
				 * so we can interact with the user to get his/her chat name.
				 */
				AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
				builder.setTitle("Your name");
				final EditText input = new EditText(v.getContext());
				input.setText(nick);
				input.setHint("Your name...");
				input.setSingleLine();
				builder.setView(input);
				builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						nick = input.getText().toString();
						intent.putExtra("ID", nick);
						
						dialog.cancel();
						startActivity(intent);
					}
				});
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});

		/**
		 * Creating Button variable to hold our ip button from main.xml
		 * Then we set the OnClickListener which let us manage clicking
		 */
		Button setIP = (Button) findViewById(R.id.ip);
		setIP.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				/**
				 * Calls AlertDialog to create a little pop up dialog
				 * so we can interact with the user to get the IP.
				 */
				AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
				builder.setTitle("Set server IP");
				final EditText input = new EditText(v.getContext());
				input.setHint("Server IP");
				input.setText(ip);
				input.setSingleLine();
				builder.setView(input);
				builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						ip = input.getText().toString();
						dialog.cancel();
					}
				});
				AlertDialog dialog = builder.create();
				dialog.show();
			}
		});
		
		/*
		 * TODO
		 * Enable Options button with settings
		 * you feel are needed
		 */
	}
}